adTempus API
Client API / Examples / Retrieve and Execute a Job
In This Topic
    Retrieve and Execute a Job
    In This Topic

    This example demonstrates how to fetch a job (given its name), submit the job for execution, wait until execution completes, and display the final status for the job.

    Be sure to change "My Test Job" to the name of a valid job on your server.

    'Connect to the local adTempus server, using Windows authentication.
    Using connection As Scheduler = Scheduler.Connect()
        'Create a DataContext to work in. All object operations take place within this context.
        'Use a Using block so the context is disposed when we finish with it
        Using context As DataContext = connection.NewDataContext()
            'Fetch the job named "My Test Job"
            Dim job = context.GetJob("My Test Job")
            If job Is Nothing Then
                'job not found
                Return
            End If
    
            Dim settings = New JobExecutionSettings()
            settings.Options = JobExecutionOptions.ForceNewInstance
            Dim result = job.Execute(settings)
            If Not result.JobSubmitted Then
                System.Diagnostics.Debug.WriteLine("Submission failed")
                For Each message As Message In result.Messages
                    System.Diagnostics.Debug.WriteLine(message.ToString())
                Next
                Return
            End If
    
            'The Instances collection contains all the instances created for this request.
            '(there will be more than one instance if the job was configured to run on Agents).
            'for this example we assume there's only one instance for the job
            Dim jobInstance = result.Instances(0)
            System.Diagnostics.Debug.WriteLine("Submitted instance " & jobInstance.InstanceID.ToString())
    
            'wait until the job finishes
            Do
                System.Threading.Thread.Sleep(5000)
    
                'refresh to get the most recent status for the instance
                jobInstance.Refresh()
            Loop While jobInstance.IsRunning
    
            System.Diagnostics.Debug.WriteLine("Instance finished with status " & JobStatusHelpers.GetJobStatusDescription(jobInstance.Status))
    
    
            'the JobExecutionSettings.ExecutionRequestID value is associated with all instances
            'submitted by this request and can be used to fetch all instances from this request if they
            'are needed later on:
            Dim instances = connection.JobServices.GetInstancesForRequest(settings.ExecutionRequestID)
        End Using
    End Using
    //Connect to the local adTempus server, using Windows authentication.
    using (Scheduler connection = Scheduler.Connect())
    {
        //Create a DataContext to work in. All object operations take place within this context.
        //Use a Using block so the context is disposed when we finish with it
        using (DataContext context = connection.NewDataContext())
        {
            //Fetch the job named "My Test Job"
            var job = context.GetJob("My Test Job");
            if (null == job)
            {
                //job not found
                return;
            }
    
            var settings = new JobExecutionSettings();
            settings.Options = JobExecutionOptions.ForceNewInstance;
            var result = job.Execute(settings );
            if (!result.JobSubmitted)
            {
                System.Diagnostics.Debug.WriteLine("Submission failed");
                foreach (Message message in result.Messages)
                {
                    System.Diagnostics.Debug.WriteLine(message.ToString());
                }
                return;
            }
            
            //The Instances collection contains all the instances created for this request.
            //(there will be more than one instance if the job was configured to run on Agents).
            //for this example we assume there's only one instance for the job
            var jobInstance = result.Instances[0];
            System.Diagnostics.Debug.WriteLine("Submitted instance " + jobInstance.InstanceID.ToString());
    
            //wait until the job finishes
            do
            {
                System.Threading.Thread.Sleep(5000);
    
                //refresh to get the most recent status for the instance
                jobInstance.Refresh();
            } while (jobInstance.IsRunning);
    
            System.Diagnostics.Debug.WriteLine("Instance finished with status " + JobStatusHelpers.GetJobStatusDescription(jobInstance.Status));
    
    
            //the JobExecutionSettings.ExecutionRequestID value is associated with all instances
            //submitted by this request and can be used to fetch all instances from this request if they
            //are needed later on:
            var instances = connection.JobServices.GetInstancesForRequest(settings.ExecutionRequestID);
        }
    }
    See Also